Multi-lingual Application

Add Dropdown List To Choose Language Of Application

Description
This customization shows how to add a dropdown list to change language(UI culture) of your application.
Variables
Applies to
Page class
Code
 
''' 
''' The MyPage_Load sub handles load event for page.
''' 
''' The object that raised the load event.
''' The object that contains the event data of the load event.
Private Sub MyPage_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    If Not Me.Page.IsPostBack Then

        ' Get the resource directory of the application. It is "bin" for .NET Framework 1.1 applications
        ' and "App_GlobalResources" for .NET Framework 2.0 applications
        Dim resourceDir As String = ""
        Dim runtimeVersion As String = System.Runtime.InteropServices.RuntimeEnvironment.GetSystemVersion()
        If runtimeVersion.IndexOf("v2") = -1 Then
            resourceDir = "bin"
        Else
            resourceDir = "App_GlobalResources"
        End If

        ' Get full path of resource directory
        Dim resourcePath As String = Me.Request.PhysicalApplicationPath() & resourceDir & "\"

        If System.IO.Directory.Exists(resourcePath) Then

            ' Find all the resource files of the type AppName.culture.resx
            Dim resourceFiles() As String = System.IO.Directory.GetFiles(resourcePath, "*.*.resx")
            Dim resourceFile As String = ""
            For Each resourceFile In resourceFiles

                ' Get the file name
                Dim finfo As  System.IO.FileInfo = New  System.IO.FileInfo(resourceFile)
                Dim fileName As String = finfo.Name

                ' Split the filename by the "."
                Dim splitBy As String ="."
                Dim namePieces As String() = fileName.Split(splitBy.ToCharArray())

                Dim length As Integer = namePieces.Length
                Dim index As Integer = length - 2

                ' Get the culture name portion of the filename like "en-US"
                Dim cultureName As String = namePieces(index)

                'Create a CultureInfo object to represent the culture
                Dim culutreInfo As System.Globalization.CultureInfo = New System.Globalization.CultureInfo(cultureName)

                ' Populate the cultures list
                Dim li As ListItem = New ListItem(culutreInfo.DisplayName, culutreInfo.Name)
                If Not Me.LanguageList.Items.Contains(li) Then
                    Me.LanguageList.Items.Add(li)  
                        
                    ' Select the current culture of the application
                    If System.Threading.Thread.CurrentThread.CurrentUICulture.Name = culutreInfo.Name Then 
                        li.Selected = True
                    End If  
                End If
            Next resourceFile
        End If
    End If
End Sub
     
Applies to
Page class
Code
 
''' 
''' The LanguageList_SelectedIndexChanged sub handles SelectedIndexChanged event for the LanguageList dropdown list.
''' 
''' The object that raised the SelectedIndexChanged event.
''' The object that contains the event data of the SelectedIndexChanged event.
Private Sub LanguageList_SelectedIndexChanged(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles LanguageList.SelectedIndexChanged

    ' Base Page sets the culture for the current thread by reading the two session variables 
    ' "AppCulture" and "AppCultureUI"
    Session("AppCulture") = Me.LanguageList.SelectedItem.Value
    Session("AppCultureUI") = Me.LanguageList.SelectedItem.Value

    'The culture is applied to the page in Page_Init of BasePage. Therefore, redirecting back to same page.
    Me.Page.Response.Redirect(Request.Url.PathAndQuery)		
End Sub
     

Terms of Service Privacy Statement